﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HMS.BL;
using HMS.Common;
using HMS.DL;
using System.Data;

namespace HMS.PL.Forms
{
    public partial class ReportsCallback : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsPostBack == false)
            {

                string strOpern = String.IsNullOrEmpty(Request.QueryString["operation"]) ? "" : Request.QueryString["operation"].ToString();

                if (strOpern == null || strOpern == "")
                {
                    strOpern = Request.Form["oper"];
                }

                ReportData objReportData = new ReportData();
                if (String.IsNullOrEmpty(Request.QueryString["reconstruct"]) == false)
                {
                    return;
                }

                switch (strOpern)
                {

                    case "get_gridColdata"://done
                        objReportData.ListId = String.IsNullOrEmpty(Request.QueryString["list_id"]) ? 0 : Convert.ToInt32(Request.QueryString["list_id"].ToString());
                        objReportData.dimTableName = Request.QueryString["dimtable_name"].ToString();
                        objReportData.bShowSource = false;
                        Response.Write(objReportData.GetGridColdata());
                        break;
                    //case "get_griddata":
                    //    objMasterData.ListId = String.IsNullOrEmpty(Request.QueryString["list_id"]) ? 0 : Convert.ToInt32(Request.QueryString["list_id"].ToString());
                    //    objMasterData.dimTableName = Request.QueryString["dimtable_name"].ToString();
                    //    objMasterData.sidx = String.IsNullOrEmpty(Request.Params["sidx"]) ? "" : Convert.ToString(Request.Params["sidx"].ToString());
                    //    objMasterData.sord = String.IsNullOrEmpty(Request.Params["sord"]) ? "desc" : Convert.ToString(Request.Params["sord"].ToString());
                    //    objMasterData.rows = String.IsNullOrEmpty(Request.Params["rows"]) ? 10 : Convert.ToInt32(Request.Params["rows"].ToString());
                    //    objMasterData.page = String.IsNullOrEmpty(Request.Params["page"]) ? 1 : Convert.ToInt32(Request.Params["page"].ToString());
                    //    objMasterData._search = String.IsNullOrEmpty(Request.Params["_search"]) ? false : Convert.ToBoolean(Request.Params["_search"]);
                    //    objMasterData.query = String.IsNullOrEmpty(Request.Params["searchQuery"]) ? "" : Convert.ToString(Request.Params["searchQuery"]);
                    //    Response.Write(objMasterData.GetGriddata());
                    //    break;

                    case "get_gridColdataRpt"://done
                        objReportData.ListId = String.IsNullOrEmpty(Request.QueryString["list_id"]) ? 0 : Convert.ToInt32(Request.QueryString["list_id"].ToString());
                        objReportData.dimTableName = Request.QueryString["dimtable_name"].ToString();
                        objReportData.selectedColumns = String.IsNullOrEmpty(Request.Params["strcol"]) ? "" : Convert.ToString(Request.Params["strcol"].ToString());
                        objReportData.bShowSource = true;
                        Response.Write(objReportData.GetGridColdata());
                        break;
                    case "get_griddataRpt"://done
                        objReportData.ListId = String.IsNullOrEmpty(Request.QueryString["list_id"]) ? 0 : Convert.ToInt32(Request.QueryString["list_id"].ToString());
                        objReportData.dimTableName = Request.QueryString["dimtable_name"].ToString();
                        objReportData.sidx = String.IsNullOrEmpty(Request.Params["sidx"]) ? "" : Convert.ToString(Request.Params["sidx"].ToString());
                        objReportData.sord = String.IsNullOrEmpty(Request.Params["sord"]) ? "desc" : Convert.ToString(Request.Params["sord"].ToString());
                        objReportData.rows = String.IsNullOrEmpty(Request.Params["rows"]) ? 10 : Convert.ToInt32(Request.Params["rows"].ToString());
                        objReportData.page = String.IsNullOrEmpty(Request.Params["page"]) ? 1 : Convert.ToInt32(Request.Params["page"].ToString());
                        objReportData.SourceQuery = String.IsNullOrEmpty(Request.Params["SourceQuery"]) ? "" : Convert.ToString(Request.Params["SourceQuery"]);
                        objReportData.queryRel = String.IsNullOrEmpty(Request.Params["queryRel"]) ? "" : Convert.ToString(Request.Params["queryRel"]);
                        objReportData.selectedColumns = String.IsNullOrEmpty(Request.Params["strcol"]) ? "" : Convert.ToString(Request.Params["strcol"].ToString());
                        //DataSet dsRpt;
                        Response.Write(objReportData.GetGriddata());
                        break;
                    case "export_excel":
                        DataSet dsRpt1 = GetRptDataset();
                        System.IO.MemoryStream xlsStream = Export.CreateExcelDocument(dsRpt1);
                        try
                        {
                            Response.Clear();
                            Response.ClearHeaders();
                            Response.ClearContent();
                            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                            Response.BinaryWrite(xlsStream.ToArray());
                            //Response.WriteFile(Server.MapPath("Excel.xlsx"));
                            Response.Flush();
                            Response.End();
                        }
                        catch (System.Threading.ThreadAbortException ex)
                        {

                        }
                        break;
                    case "export_pdf":
                        DataSet dsRpt2 = GetRptDataset();
                        System.IO.MemoryStream pdfStream = Export.CreatePDFDocument(dsRpt2.Tables[0]);
                        try
                        {
                            Response.Clear();
                            Response.ClearContent();
                            Response.ClearHeaders();
                            Response.ContentType = "application/pdf";
                            Response.AppendHeader("Content-Disposition", "attachment; filename=PDFReport.pdf");
                            Response.BinaryWrite(pdfStream.ToArray());
                            Response.End();
                        }
                        catch (System.Threading.ThreadAbortException ex)
                        {

                        }
                        break;
                    default:
                        // Response.Write(objMasterData.GetMaster());
                        break;
                }
            }
        }

        private DataSet GetRptDataset() //done
        {
            DataSet dsRpt2 = null;
            try
            {
                ReportData objReportData = new ReportData();
                objReportData.ListId = String.IsNullOrEmpty(Request.QueryString["list_id"]) ? 0 : Convert.ToInt32(Request.QueryString["list_id"].ToString());
                objReportData.dimTableName = Request.QueryString["dimtable_name"].ToString();
                objReportData.SourceQuery = String.IsNullOrEmpty(Request.Params["SourceQuery"]) ? "" : Convert.ToString(Request.Params["SourceQuery"]);
                objReportData.queryRel = String.IsNullOrEmpty(Request.Params["queryRel"]) ? "" : Convert.ToString(Request.Params["queryRel"]);
                objReportData.selectedColumns = String.IsNullOrEmpty(Request.Params["selectedColumns"]) ? "" : Convert.ToString(Request.Params["selectedColumns"].ToString());
                dsRpt2 = new DataSet();
                dsRpt2 = objReportData.GetGriddataDummy();
            }
            catch (Exception ex)
            {

            }          
            return dsRpt2;            
        }
    }
}